﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using System.Web;
using Antu.WorkFlowFactory;
using System.Data;

namespace WebSys.UI.OfficeFlow
{
    public class MenuGroupSkin : MPBasicTemplate
    {
        FlowFactoryProvider _FlowFactoryProvider = (FlowFactoryProvider)ProviderService.ProviderServiceMange.GetProvider("FlowFactoryProvider");
        Button btnQuery;
        TextBox txtSeach;
        MPRepeater MPRepeaterList;
        protected string UserId
        {
            get { if (base.Page.Session["UserId"] != null) { return base.Page.Session["UserId"].ToString(); } else { return string.Empty; } }
            set { base.Page.Session["UserId"] = value; }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            InitControl();
            if (!Page.IsPostBack)
            {
                
                BindData();
            }
            Button saveset = (Button)FindControl("saveset");
            saveset.Click += new EventHandler(saveset_Click);
        }
        private void InitControl()
        {
            MPRepeaterList = (MPRepeater)FindControl("MPRepeaterList");
        }

        protected void BindData() {
            MPRepeaterList.ItemDataBound += new RepeaterItemEventHandler(MPRepeaterList_ItemDataBound);
            DataTable dt = _FlowFactoryProvider.GetGroupMenu("0", "", "");
            MPRepeaterList.DataSource = dt;
            MPRepeaterList.DataBind();
        }

        protected void MPRepeaterList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            string strid = base.Page.Request["groupid"];
            Label showtr = (Label)e.Item.FindControl("showtr");
            Label idtib = (Label)e.Item.FindControl("idtib");
            if ((e.Item.ItemIndex + 1) % 2 == 0)
            {
                showtr.Text = "</tr><tr>";
            }
            MPRepeater sMPRepeaterList = (MPRepeater)e.Item.FindControl("sMPRepeaterList");
            DataTable dt = _FlowFactoryProvider.GetGroupMenu("1", strid, idtib.Text.Trim());
            sMPRepeaterList.DataSource = dt;
            sMPRepeaterList.DataBind();
        }

        protected void saveset_Click(object sender, EventArgs e)
        {
            string strid = base.Page.Request["groupid"];
            for (int temp = 0; temp < MPRepeaterList.Items.Count; temp++)
            {
                MPRepeater sMPRepeaterList = (MPRepeater)MPRepeaterList.Items[temp].FindControl("sMPRepeaterList");
                for (int index = 0; index < sMPRepeaterList.Items.Count; index++)
                {
                    Label sidtib = (Label)sMPRepeaterList.Items[index].FindControl("sidtib");
                    CheckBox checkselect = (CheckBox)sMPRepeaterList.Items[index].FindControl("checkselect");
                    int usercount = Convert.ToInt32(_FlowFactoryProvider.SetGroupMenu(strid, sidtib.Text.Trim()));
                    if (checkselect.Checked)
                    {
                        if (usercount == 0)
                        {
                            _FlowFactoryProvider.UpdateGroupMenu("0", strid, sidtib.Text.Trim());
                        }
                    }
                    else
                    {
                        if (usercount > 0)
                        {
                            _FlowFactoryProvider.UpdateGroupMenu("1", strid, sidtib.Text.Trim());
                        }
                    }
                }
            }
            Antu.Utilities.UtilitiesPage.RegJsScript(string.Format("window.location.href('MenuGroup.aspx?groupid={0}');", strid));
        }

    }
}


